package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Bumen;
import org.zhigong.entity.Page;
import org.zhigong.service.BumenService;

import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;


@Controller
public class BumenController {

    @Autowired
    private BumenService bumenService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/bumens", method = RequestMethod.GET)
    public String init(){
        return "bumen";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/bumens",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Bumen>> search(SysSearch sysSearch){
        SysResult<List<Bumen>> sysResult;

        if(sysSearch.getPageNumber() < 0){
            try{
                List<Bumen> bumenList = bumenService.getBumenListAll();
                sysResult = new SysResult<List<Bumen>>(true, bumenList);
            }catch (Exception e){
                sysResult = new SysResult<List<Bumen>>(false, e.getMessage());
            }
        } else{
            Bumen bumen = new Bumen();
            String condS = sysSearch.getCondition();
            bumen.setMc(condS);
            bumen.getPage().setCurrentPage(sysSearch.getCurrentPage());
            bumen.getPage().setPageNumber(sysSearch.getPageNumber());

            try{
                List<Bumen> bumenList = bumenService.getBumenList(bumen);
                if(bumenList.size() > 0){
                    bumenList.get(0).setPage(bumen.getPage());
                }
                sysResult = new SysResult<List<Bumen>>(true, bumenList);
            }catch (Exception e){
                sysResult = new SysResult<List<Bumen>>(false, e.getMessage());
            }
        }
        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/bumens",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Bumen bumen){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = bumenService.addBumen(bumen);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/bumens/{bmbh}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("bmbh") int bmbh){
        SysResult<Integer> sysResult;
        try{
            Boolean del = bumenService.deleteBumen(bmbh);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/bumens/{bmbh}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Bumen bumen){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = bumenService.updateBumen(bumen);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/bumens/{bmbh}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Bumen> view(@PathVariable("bmbh") int bmbh){
        SysResult<Bumen> sysResult;
        try{
            Bumen bumen = bumenService.getBumenByBmbh(bmbh);
            sysResult = new SysResult<Bumen>(true, bumen);
        }catch(Exception e){
            sysResult = new SysResult<Bumen>(false, e.getMessage());
        }
        return sysResult;
    }
}
